package com.aso114.project.mvp.alipay;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.alipay.sdk.app.H5PayCallback;
import com.alipay.sdk.app.PayTask;
import com.alipay.sdk.util.H5PayResultModel;
import com.aso114.project.R;
import com.aso114.project.mvp.activity.LoginActivity;
import com.aso114.project.mvp.activity.MainActivity;
import com.aso114.project.mvp.activity.OpenVipActivity;
import com.aso114.project.mvp.activity.StarActivity;
import com.aso114.project.mvp.activity.VipCoreActivity;
import com.aso114.project.util.ActivityCollector;
import com.aso114.project.util.ToastUtils;

public class H5PayDemoActivity extends Activity {

	private WebView mWebView;
	String orderNo = "";
//	Intent intent = new Intent(MainActivity.REFRESH);
	Intent intent;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.h5pay_layout);

		orderNo = getIntent().getStringExtra("orderNo");
		mWebView = (WebView)findViewById(R.id.web);

		WebSettings settings = mWebView.getSettings();
		settings.setRenderPriority(RenderPriority.HIGH);
		settings.setJavaScriptEnabled(true);
		settings.setSavePassword(false);
		settings.setJavaScriptCanOpenWindowsAutomatically(true);
		settings.setMinimumFontSize(settings.getMinimumFontSize() + 8);
		settings.setAllowFileAccess(false);
		settings.setTextSize(WebSettings.TextSize.NORMAL);
		mWebView.setVerticalScrollbarOverlay(true);
		mWebView.setWebViewClient(new MyWebViewClient());
		mWebView.loadUrl("http://pay.sjsj.cn/alipay/index.html?orderNo="+orderNo+"&appKeyId=100001&source=exam&platform=Android");
	}

	@Override
	public void onBackPressed() {
		if (mWebView.canGoBack()) {
			mWebView.goBack();
		} else {
			finish();
		}
	}

	@Override
	public void finish() {
		super.finish();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	private class MyWebViewClient extends WebViewClient {

		@Override
		public boolean shouldOverrideUrlLoading(final WebView view, String url) {
			if (!(url.startsWith("http") || url.startsWith("https"))) {
				return true;
			}

//			/**
//			 * 推荐采用的新的二合一接口(payInterceptorWithUrl),只需调用一次
//			 */
			final PayTask task = new com.alipay.sdk.app.PayTask(H5PayDemoActivity.this);
			boolean isIntercepted = task.payInterceptorWithUrl(url, true, new com.alipay.sdk.app.H5PayCallback() {
				@Override
				public void onPayResult(final H5PayResultModel result) {
					Message msg = new Message();
					msg.what = Integer.valueOf(result.getResultCode());
					mHandler.sendMessage(msg); // 向Handler发送消息,更新UI
					final String url=result.getReturnUrl();
					if(!TextUtils.isEmpty(url)){
						H5PayDemoActivity.this.runOnUiThread(new Runnable() {
							@Override
							public void run() {
								view.loadUrl(url);
							}
						});
					}
				}
			});

			/**
			 * 判断是否成功拦截
			 * 若成功拦截，则无需继续加载该URL；否则继续加载
			 */
			if(!isIntercepted)
				view.loadUrl(url);
			return true;
		}
	}


	private Handler mHandler = new Handler(){

		public void handleMessage(Message msg) {
			switch (msg.what) {
				case 6001:
				    gotoactivity("1");//1是失败 2是成功
					break;
				case 9000:
					gotoactivity("2");//1是失败 2是成功
					break;
				case 4000:
					gotoactivity("1");//1是失败 2是成功
					break;
				case 6002:
					gotoactivity("1");//1是失败 2是成功
					break;
			}
		};
	};


	private void gotoactivity(final String type){


		//充值成功就是会员
		if(type.equals("2")){

			if(OpenVipActivity.instance!=null){
				OpenVipActivity.instance.finish();
			}

			finish();

		}else{
			//充值失败，则判断之前是否会员
			//这边需要加个 判断之前是否是会员
//			intent = new Intent(H5PayDemoActivity.this, VipCoreActivity.class);
//			intent.putExtra("vipTpye","0");
//			startActivity(intent);
			finish();
		}



	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (mWebView != null) {
			mWebView.removeAllViews();
			try {
				mWebView.destroy();
			} catch (Throwable t) {
			}
			mWebView = null;
		}
	}


	class MyThread implements Runnable {
		public void run() {
			//执行数据操作，不涉及到UI
			Message msg = new Message();
			msg.what = 1;
			mHandler.sendMessage(msg); // 向Handler发送消息,更新UI
		}
	}

}
